package hash.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 3. 无重复字符的最长子串
 * @author haomin
 * @date 2022/03/16 20:17
 **/
public class Num3 {
    public static int lengthOfLongestSubstring(String s) {
        if(s == null){
            return 0;
        }
        int index = 0;
        int max = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))){
                index = Math.max(map.get(s.charAt(i))+1, index);//不重复字符串的起始位置
            }
            max = Math.max(i-index+1, max);
            map.put(s.charAt(i), i);
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("aabc"));
    }
}